package com.qxglxt.qxglxt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.qxglxt.qxglxt.dao.IRolesDao;
import com.qxglxt.qxglxt.model.Roles;
import com.qxglxt.qxglxt.service.IRoleService;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/role")
public class RolesController {
    @Resource
    private IRolesDao rolesDao;

    @ApiOperation(value = "查询所有角色", notes = "查询所有角色")
    @GetMapping
    public Map<String, Object> findAll() {
        List<Roles> list = rolesDao.findAll();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @ApiOperation(value = "添加角色", notes = "添加角色")
    @PostMapping("/add")
    public Roles addRole(@RequestBody Roles r) {
        return rolesDao.save(r);
    }

    @ApiOperation(value = "更新角色信息", notes = "更新角色信息")
    @PutMapping("/update")
    public Roles editRole(@RequestBody Roles r) {
        return rolesDao.save(r);
    }

    @ApiOperation(value = "删除角色", notes = "删除角色")
    @ApiImplicitParam(name = "id", value = "角色编号", required = true, paramType = "path")
    @DeleteMapping("/{id}")
    public void delRole(@PathVariable Integer id) {
        rolesDao.deleteById(id);
    }

    @ApiOperation(value = "分配角色权限",notes = "分配角色权限" )
    @PostMapping("/setRoleModules")
    public void setRoleModules(String RoleId,@RequestParam List<String> modulesId){
        roleService.setRoleModules(RoleId, modulesId);
    }

    @ApiOperation(value = "查询用户角色",notes = "查询用户角色")
    @GetMapping("/findUserRoles/{userId}")
    public List<Roles> findUserRoles(String userId){
        return roleService.findUserRoles(userId);
    }

}